package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.AuthDto;
import com.heima.user.entity.ApAuthor;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.entity.WmUser;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.directory.AttributeInUseException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-11
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private MediaFeign mediaFeign;

    @Autowired
    private IApUserService userService;

    @Override
    public ResponseResult listByStatus(AuthDto authDto) {

        IPage<ApUserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());

        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();

        if (authDto.getStatus() != null) {

            queryWrapper.eq(ApUserRealname::getStatus, authDto.getStatus());
        }

        IPage<ApUserRealname> page1 = this.page(page, queryWrapper);

        return new PageResponseResult(authDto.getPage(), authDto.getSize(), page1.getTotal(), page1.getRecords());
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int i) {

        ApUserRealname apUserRealname = this.getById(dto.getId());

        if (i == 0) {

            apUserRealname.setStatus(2);

            apUserRealname.setReason(dto.getReason());

            this.updateById(apUserRealname);

            return ResponseResult.okResult();
        }
        ApUser byId = userService.getById(apUserRealname.getUserId());

        apUserRealname.setStatus(9);

        this.updateById(apUserRealname);

        WmUser wmUser = createWmUser(apUserRealname);

       // int i1 = 1 / 0;

        if (wmUser != null) {

            ApAuthor apAuthor = createAuthor(apUserRealname, wmUser);

            if (apAuthor != null) {

                wmUser.setApAuthorId(apAuthor.getId());

                mediaFeign.updateWmUser(wmUser);

                byId.setFlag(1);

                userService.updateById(byId);
            }
        }
        return ResponseResult.okResult();
    }

    private ApAuthor createAuthor(ApUserRealname apUserRealname, WmUser wmUser) {

        ApAuthor apAuthor = new ApAuthor();

        apAuthor.setName(apUserRealname.getName());

        apAuthor.setUserId(apUserRealname.getId());

        apAuthor.setWmUserId(wmUser.getId());

        apAuthor.setType(2);

        ResponseResult<ApAuthor> apAuthorResponseResult = articleFeign.saveApAuthor(apAuthor);

        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {

            return apAuthorResponseResult.getData();
        }
        return null;
    }

    private WmUser createWmUser(ApUserRealname apUserRealname) {

        WmUser wmUser = new WmUser();

        BeanUtils.copyProperties(apUserRealname, wmUser);

        wmUser.setId(null);
        wmUser.setApUserId(apUserRealname.getId());
        wmUser.setNickname(apUserRealname.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);

        ResponseResult<WmUser> wmUserResponseResult = mediaFeign.saveWmUser(wmUser);

        if (wmUserResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {

            return wmUserResponseResult.getData();
        }
        return null;
    }
}
